/**
 * 
 */
package de.schnocklake.android.sap.crm.datamodel;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;

import de.schnocklake.android.soap.SoapException;
import de.schnocklake.android.soap.SoapUtils;

public class Product {
	protected String endpoint = "http://crm.esworkplace.sap.com:80/sap/bc/srt/xip/sap/CRM_BTSQ_CUSTQTECRTCHKQR?sap-client=800";
	protected String username = "S0004428881";
	protected String password = "Mantila1";

	public Product(String pRODUCTGUID, String sHTEXTLARGE, String pRODUCTID,
			String lANGU) {
		super();
		PRODUCT_GUID = pRODUCTGUID;
		SHTEXT_LARGE = sHTEXTLARGE;
		PRODUCT_ID = pRODUCTID;
		LANGU = lANGU;
	}

	private String PRODUCT_GUID;
	private String SHTEXT_LARGE;
	private String PRODUCT_ID;
	private String LANGU;
	private String units[] = {};

	public String getPRODUCT_GUID() {
		return PRODUCT_GUID;
	}

	public String getSHTEXT_LARGE() {
		return SHTEXT_LARGE;
	}

	public String getPRODUCT_ID() {
		return PRODUCT_ID;
	}

	public String getLANGU() {
		return LANGU;
	}

	public String[] getUnits() {
		return units;
	}

	public void setUnits(String[] units) {
		String[] old = this.units;
		this.units = units;
		this.pcs.firePropertyChange("units", old, units);
	}

	public String[] readProductDetail() {
		Document request = readProductDetailCreateDocument();
		String req = request.asXML();
		req = req + "";
		try {
			HttpURLConnection connection;
			connection = (HttpURLConnection) new URL(
					"http://crm.esworkplace.sap.com:80/sap/bc/srt/xip/sap/CRM_PRD_MATLID_QR?sap-client=800")
					.openConnection();

			Document responseDoc = SoapUtils.request(request, connection,
					username, password);
			String resp = responseDoc.asXML();
			resp = resp + "";
			connection.disconnect();

			String baseMeasureUnitCode;
			baseMeasureUnitCode = responseDoc.selectSingleNode(
					"//Material/Common/BaseMeasureUnitCode").getText();
			return new String[] { baseMeasureUnitCode };
		} catch (SoapException e) {
			e.printStackTrace();
			return null;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

	}

	public Document readProductDetailCreateDocument() {
		DocumentFactory factory = DocumentFactory.getInstance();

		Document document = factory.createDocument();
		Element envelope = document.addElement(factory.createQName("Envelope",
				"soapenv", "http://schemas.xmlsoap.org/soap/envelope/"));
		envelope.addElement(factory.createQName("Header", "soapenv",
				"http://schemas.xmlsoap.org/soap/envelope/"));
		Element body = envelope.addElement(factory.createQName("Body",
				"soapenv", "http://schemas.xmlsoap.org/soap/envelope/"));
		Element materialCRMByIDQuery_sync = body.addElement(factory
				.createQName("MaterialCRMByIDQuery_sync", "glob",
						"http://sap.com/xi/CRM/Global2"));
		Element materialSelectionByID = materialCRMByIDQuery_sync
				.addElement("MaterialSelectionByID");
		materialSelectionByID.addElement("InternalID").addText(
				this.getPRODUCT_ID());

		return document;
	}

	// Beans stuff --- property change implementation

	private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		this.pcs.addPropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		this.pcs.removePropertyChangeListener(listener);
	}

}